using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using CrystalDecisions.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

namespace THSLib
{
    public class ReportHelper
    {
        protected static ConnectionInfo _reportConnection;
        protected static ReportDocument _reportDoc;
        //protected static String _fileName;

        public static String FileName
        {
            get
            {
                return _reportDoc.FileName;
            }
            set
            {
                _reportDoc.FileName = value;
            }
        }

        public static ReportDocument ReportDoc
        {
            get
            {
                return _reportDoc;
            }
            set
            {
                _reportDoc = value;
            }
        }

        static ReportHelper()
        {
            _reportDoc = new ReportDocument();
            _reportConnection = new ConnectionInfo();
            SetReportConnection();
        }

        private static void SetReportConnection()
        {
            RegistryWorker regW = new RegistryWorker();
            regW.SubKey = "SOFTWARE\\THS";
            regW.SubKey = regW.SubKey + "\\ConnectionString";
            String strDatabase = regW.Read("Database");
            String strServer = regW.Read("Server");
            String strUser = regW.Read("User");
            string strPassword = regW.Read("Password");

            //If can get connection info from registry
            if (!String.IsNullOrEmpty(strServer))
            {
                EnCrypto cryp = new EnCrypto();
                strPassword = cryp.Decrypt(strPassword);

                //_reportConnection = new ConnectionInfo();
                _reportConnection.ServerName = strServer;
                _reportConnection.DatabaseName = strDatabase;
                _reportConnection.UserID = strUser;
                _reportConnection.Password = strPassword;
            }
            else
            {
                //Get from configuration file
                _reportConnection.ServerName = ConfigurationManager.AppSettings["ServerName"];
                _reportConnection.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
                _reportConnection.UserID = ConfigurationManager.AppSettings["UserID"];
                _reportConnection.Password = ConfigurationManager.AppSettings["Password"];
            }
        }

        public static void ApplyReportLogon()
        {
            try
            {
                //ReportDoc.FileName = FileName;
                foreach (Table reportTable in ReportDoc.Database.Tables)
                {
                    TableLogOnInfo _tableLogonInfo = reportTable.LogOnInfo;
                    _tableLogonInfo.ConnectionInfo = _reportConnection;
                    reportTable.ApplyLogOnInfo(_tableLogonInfo);
                }

            }
            catch (Exception)
            {
                MessageError.ShowLogOnDBError();
            }

        }

        public static void SetParamValues(Hashtable paramValues)
        {
            ApplyReportLogon();
            foreach (ParameterField paramField in ReportDoc.ParameterFields)
            {
                Object objValue = paramValues[paramField.Name];
                if (objValue != null)
                    ReportDoc.SetParameterValue(paramField.Name, paramValues[paramField.Name]);
            }
        }

        public static void DisplayReport(Hashtable paramValues)
        {
            SetParamValues(paramValues);
            guiTHSReportViewer _guiTHSReportViewer = new guiTHSReportViewer();
            _guiTHSReportViewer.THSReportViewer.ReportSource = ReportDoc;
            _guiTHSReportViewer.Show();         
                    
        }

        public static void setSource(string sTableName, DataTable tb)
        {
            _reportDoc.SetDataSource(tb);
        }
    }
}
